/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const cheerio = require('cheerio');
const urlTool = require('url');

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class Ipoboutique extends ArticleHrefAdapter {


    parseHtml(html, linkRule) {
        //将解析后的内容加载进解析器
        let $ = cheerio.load(html);

        let contents = [];
        let that = this;
        let rangeText = '';
        $(this.linkRule).each(function () {
            let title = '';
            let trStr = $(this).html();
            if (trStr != '<td></td>') {
                let $$ = cheerio.load(trStr);
                let tdArr = trStr.split("</td>");
                let tdIndex = 0;
                for (let i = 0; i < tdArr.length; i++) {
                    tdIndex = i;
                    if (tdIndex == 0) {
                        title += '<strong>Company: </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 1) {
                        title += '<strong>Symbol: </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 2) {
                        title += '<strong>Price at Filing: </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 3) {
                        title += '<strong>Shares: </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 4) {
                        title += '<strong>Trade Date: </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 5) {
                        title += '<strong>Underwriter(s): </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    } else if (tdIndex == 6) {
                        title += '<strong>Co-Manager(s): </strong>' + tdArr[i].replace(/<td([\s\S]*?)>/g, '') + '<br/>';
                    }
                    // tdIndex ++;
                }
                ;

                if (!/\([0-9]+\)/g.test(title) && !/（[0-9]+）/g.test(title)) {
                    let href = '';
                    if ($(this).is('a') && $(this).attr('href')) {
                        href = urlTool.resolve(that.requestUrl, $(this).attr('href'));
                    }

                    if (title.length != 0 || href.length != 0) {
                        contents.push({
                            title: title,
                            href: href
                        });
                        rangeText += title + href;
                    }
                }
            }


        });

        return {
            rangeText: rangeText,
            contents: contents
        };
    }


}
